extends Node

enum BUS{MASTER, SFX, BGM}

signal sfx_finish

@onready var sfx: Node = $SFX
@onready var bgm_player: AudioStreamPlayer = $BGMPlayer

func play_sfx(_name: String) -> void :
	var player: = sfx.get_node(_name) as AudioStreamPlayer
	if not player:
		return
	player.play()
	var pitch = 1
	player.pitch_scale = randf_range(pitch - 0.2, pitch + 0.2)
	sfx_finish.emit()


func play_bgm(stream: AudioStream) -> void :
	if bgm_player.stream == stream and bgm_player.playing:
		return
	bgm_player.stream = stream
	bgm_player.play()

func setup_ui_sounds(node: Node) -> void :
	var button: = node as Button
	if button:
		button.pressed.connect(play_sfx.bind("UIPress"))
		button.focus_entered.connect(play_sfx.bind("UIFocus"))
		button.mouse_entered.connect(button.grab_focus)

	var slider: = node as Slider
	if slider:
		slider.value_changed.connect(play_sfx.bind("UIPress").unbind(1))
		slider.focus_entered.connect(play_sfx.bind("UIFocus"))
		slider.mouse_entered.connect(slider.grab_focus)

	for child in node.get_children():
		setup_ui_sounds(child)

func stop_sfx(_name: String) -> void :
	var player: = sfx.get_node(_name) as AudioStreamPlayer
	player.stop()

func stop_all_sfx() -> void:
	for i:AudioStreamPlayer in sfx.get_children():
		i.stop()

func stop_music() -> void:
	bgm_player.stop()

func get_volume(bus_index: int) -> float:
	var db: = AudioServer.get_bus_volume_db(bus_index)
	return db_to_linear(db)


func set_volume(bus_index: int, v: float) -> void :
	var db: = linear_to_db(v)
	AudioServer.set_bus_volume_db(bus_index, db)
